﻿using OpenAI.Chat;
using OpenAI.Models;
using OpenAI.Threads;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Message = OpenAI.Chat.Message;

namespace OpenAI
{
    public class OpenAI_DotNet
    {

        public OpenAIAuthentication org = new OpenAIAuthentication("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", "org-RapwMLOq73H4V9lttZAVkWve");
        public async Task<string> GreateOpenAIDemoAsync()
        {
            var settings = new OpenAIClientSettings(domain: "https://openkey.cloud");
            using var api = new OpenAIClient("sk-4JTX4rUegqhrbVArfdJ8R3eMoScwAM85ZXUGs3yfrxeL98a7", settings);
            var thread = await api.ThreadsEndpoint.CreateThreadAsync();
            var request = new CreateMessageRequest("Hello world!");
            var message = await api.ThreadsEndpoint.CreateMessageAsync(thread.Id, request);
            // OR use extension method for convenience!
            //message = await thread.CreateMessageAsync("Hello World!");
            Console.WriteLine($"{message.Id}: {message.Role}: {message.PrintContent()}");
            return message;
        }

        public async Task<string> CreateChat() 
        {
            using var api = new OpenAIClient("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");
            var messages = new List<Message>
                {
                    new Message(Role.User, "请帮我查找一下物料型号为VS-16CTQ100-M3的相关资料?"),
                };
            var chatRequest = new ChatRequest(messages, Model.GPT3_5_Turbo);
            var response = await api.ChatEndpoint.GetCompletionAsync(chatRequest);
            var choice = response.FirstChoice;
            var result = $"[{choice.Index}] {choice.Message.Role}: {choice.Message} | Finish Reason: {choice.FinishReason}";
            Console.WriteLine(result);
            return result;
        }


      
    }
}
